<template>
  <div>
    <!-- 搜索条件区域 -->
    <el-card class="search-card" style="margin-bottom: 0">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="搜索类型">
          <el-select
            v-model="searchForm.searchType"
            placeholder="请选择搜索类型"
          >
            <el-option label="租户编号" value="id"></el-option>
            <el-option label="公司名称" value="tenantName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键词">
          <el-input
            v-model="searchForm.keyword"
            placeholder="请输入关键词"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="租户编号" width="180">
        </el-table-column>
        <el-table-column prop="tenantName" label="公司名称" width="180">
        </el-table-column>
        <el-table-column prop="createTime" label="开通日期" width="180">
        </el-table-column>
        <el-table-column prop="tenantTrialEndDate" label="到期时间" width="180">
        </el-table-column>
        <el-table-column label="租户状态" width="120">
          <template slot-scope="scope">
            {{ scope.row.tenantStatus == 0 ? "正常" : "过期" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="getInfo(scope.$index, scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 分页组件 -->
    <div style="margin-top: 10px; text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <!-- 详情对话框 -->
    <el-dialog
      title="租户详细信息"
      :visible.sync="dialogVisible"
      width="600px"
      @close="closeDialog"
    >
      <el-form :model="tenantInfo" label-width="100px" :disabled="true">
        <el-form-item label="租户编号">
          <el-input v-model="tenantInfo.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="公司名称">
          <el-input v-model="tenantInfo.tenantName" disabled></el-input>
        </el-form-item>
        <el-form-item label="开通日期">
          <el-input v-model="tenantInfo.createTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="到期时间">
          <el-input v-model="tenantInfo.tenantTrialEndDate" disabled></el-input>
        </el-form-item>
        <el-form-item label="租户状态">
          <el-input v-model="tenantInfo.tenantStatusText" disabled></el-input>
        </el-form-item>
        <el-form-item label="租户金额">
          <el-input v-model="tenantInfo.tenantLogMoney" disabled></el-input>
        </el-form-item>
        <el-form-item label="增加天数">
          <el-input
            v-model="tenantInfo.tenantLogIncreaseDays"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tenantApi from "@/http/tenantApi";

export default {
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
      },
      searchForm: {
        searchType: "tenantName",
        keyword: "",
      },
      tableData: [],
      total: 0,
      dialogVisible: false, // 控制对话框显示隐藏
      tenantInfo: {
        // 存储租户详细信息
        id: "",
        tenantName: "",
        createTime: "",
        tenantTrialEndDate: "",
        tenantStatus: 0,
        tenantStatusText: "",
        tenantLogMoney: 0,
        tenantLogIncreaseDays: 0,
      },
    };
  },
  methods: {
    getInfo(index, row) {
      // 获取租户详细信息
      tenantApi
        .getTenantInfoById(row.id)
        .then((res) => {
          this.tenantInfo = res;
          // 根据状态值设置状态文本
          this.tenantInfo.tenantStatusText =
            this.tenantInfo.tenantStatus === 0 ? "正常" : "过期";
          this.dialogVisible = true; // 显示对话框
        })
        .catch((error) => {
          console.error("获取租户信息失败:", error);
          this.$message.error("获取租户信息失败");
        });
    },
    closeDialog() {
      // 关闭对话框时重置数据
      this.tenantInfo = {
        id: "",
        tenantName: "",
        createTime: "",
        tenantTrialEndDate: "",
        tenantStatus: 0,
        tenantStatusText: "",
        tenantLogMoney: 0,
      };
    },
    //查询获得全部过期租户
    getAllExpiredTenant() {
      tenantApi.getAllExpiredTenant(this.page).then((res) => {
        this.tableData = res.list;
        this.total = res.total; // 设置总记录数
      });
    },
    // 搜索功能
    handleSearch() {
      const params = {
        ...this.page,
        searchType: this.searchForm.searchType,
        keyword: this.searchForm.keyword,
      };

      tenantApi.searchExpiredTenant(params).then((res) => {
        this.tableData = res.list;
        this.total = res.total; // 设置总记录数
      });
    },
    // 重置搜索条件
    resetSearch() {
      this.searchForm.searchType = "tenantName";
      this.searchForm.keyword = "";
      this.getAllExpiredTenant();
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getAllExpiredTenant(); // 重新加载数据
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getAllExpiredTenant(); // 重新加载数据
    },
  },
  created() {
    this.getAllExpiredTenant();
  },
};
</script>

<style scoped>
.search-card {
  margin-bottom: 0;
  border-radius: 4px;
  box-shadow: none;
  background-color: #fff;
}

.search-card .el-form-item {
  margin-bottom: 0;
}

.search-card + .el-table {
  margin-top: 0;
}
</style>
